private ["_ctrl","_ok","_XD_display","_mission_id","_current_mission","_mindex","_index","_i","_str","_hour","_rain","_overcast","_matched","_available_missions"];
_mission_id = ["current_mission_id",-1] call XNetGetJIP; 
if (_mission_id > 0) then {
	hint "A mission is already running, it will be canceled if you select another!";
};

disableserialization;
if (dialog) then {closeDialog 0};

_available_missions = ["available_missions",[]] call XNetGetJIP;

_ok = createDialog "XD_MISSIONSDIALOG";
_XD_display = uiNameSpace GetVariable "XD_MISSIONS_DIALOG";

_ctrl = _XD_display displayCtrl 1001;
_mindex = _mission_id;
_current_mission = -1;
If (count _available_missions > 0) then {
	{
		_index = _ctrl lbAdd (_x select 1);
		if (_mindex == -1) then {_mindex = _index};
	} forEach (["available_missions",[]] call XNetGetJIP);
	// We don't make the combo box selection here because it triggers a script update which can interfere with the initialization
	_current_mission = (_available_missions) select _mindex;
};
DM_DIALOG_MISSIONID = _mindex;
// Content of the mission array:
// ["full_file_path","Mission_Name","Mission_Desc","Mission_Keywords",Mission_Chapters,"Mission_FolderName","Mission_StartTime:[h,m]",Mission_StartRain,Mission_StartOvercast]
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"request_mission.sqf","Current Mission",_current_mission];

_ctrl = _XD_display displayCtrl 2007;
_str = _current_mission select 2;
_ctrl ctrlSetText _str;

_ctrl = _XD_display displayCtrl 2008;
_str = _current_mission select 3;
_ctrl ctrlSetText _str;

_ctrl = _XD_display displayCtrl 2009;
_str = str(_current_mission select 4);
_ctrl ctrlSetText _str;

If (_mission_id <= 0) then {
	_ctrl = _XD_display displayCtrl 5001;
	_ctrl ctrlEnable false;
};

_hour =  floor((_current_mission select 6) select 0);
_ctrl = _XD_display displayCtrl 4001;
_mindex = 0;
For [{_i = 0},{_i < 24},{_i = _i + 1}] do {
	_str = str(_i);
	if (_i < 10) then {_str = "0" + _str;};
	_str = _str + ":00";
	_index = _ctrl lbAdd _str;
	if (_i == _hour) then {_mindex = _index};
};
_ctrl lbSetCurSel _mindex;
DM_DIALOG_TIME = _mindex;

_rain = _current_mission select 7;
_overcast = _current_mission select 8;
_ctrl = _XD_display displayCtrl 4002;
_mindex = 0;
{
	_matched = false;
	_index = _ctrl lbAdd (_x select 0);
	if (_rain >= 0) then {
		if ((_x select 1) <= _rain) then {_matched = true;};
	};
	if ((_overcast >= 0) And _matched) then {
		_matched = false;
		if ((_x select 2) <= _overcast) then {_matched = true;};
	};
	if (_matched) then {_mindex = _index;};
}foreach d_mission_dlg_weather;
_ctrl lbSetCurSel _mindex;
DM_DIALOG_WEATHER = _mindex;

_ctrl = _XD_display displayCtrl 4003;
_mindex = 0;
{
	_index = _ctrl lbAdd (_x select 0);
}foreach d_mission_dlg_power;
_ctrl lbSetCurSel 1;
DM_DIALOG_POWER = 1;

_ctrl = _XD_display displayCtrl 4004;
_mindex = 0;
{
	_index = _ctrl lbAdd (_x select 0);
}foreach d_mission_dlg_reinforcement;
_ctrl lbSetCurSel 1;
DM_DIALOG_REINFORCEMENT = 1;

_ctrl = _XD_display displayCtrl 4005;
_mindex = 0;
{
	_index = _ctrl lbAdd (_x select 0);
	if (((_x select 1) >= 0) && (_mindex == 0)) then { _mindex = _index; };
}foreach d_mission_dlg_airsupport;
_ctrl lbSetCurSel _mindex;
DM_DIALOG_AIRSUPPORT = _mindex;

_ctrl = _XD_display displayCtrl 4006;
_mindex = 1;
{
	_index = _ctrl lbAdd (_x select 0);
	if ((_x select 1) <= KRON_UPS_sharedist) then {_mindex = _index;};
}foreach d_mission_dlg_communication;
_ctrl lbSetCurSel _mindex;
DM_DIALOG_COMM = _mindex;

_ctrl = _XD_display displayCtrl 4007;
_mindex = 0;
{
	_index = _ctrl lbAdd (_x select 0);
}foreach d_mission_dlg_snipers;
_ctrl lbSetCurSel 1;
DM_DIALOG_SNIPER = 1;

// We make the selection at the end only because it will trigger the script event.
If (DM_DIALOG_MISSIONID >= 0) then {
	_ctrl = _XD_display displayCtrl 1001;
	_ctrl lbSetCurSel DM_DIALOG_MISSIONID;
};